package com.jintian.smart.kernel.flowable.controller;

import com.jintian.smart.kernel.core.domain.Page;
import com.jintian.smart.kernel.core.domain.PageQuery;
import com.jintian.smart.kernel.core.web.RestResultWrapper;
import com.jintian.smart.kernel.flowable.domain.entity.ActHiTaskInst;
import com.jintian.smart.kernel.flowable.domain.query.BusinessQuery;
import com.jintian.smart.kernel.flowable.domain.query.TaskVoQuery;
import com.jintian.smart.kernel.flowable.mapper.IBusinessMapper;
import com.jintian.smart.kernel.flowable.util.DateUtil;
import io.swagger.v3.oas.annotations.Operation;
import org.beetl.sql.core.page.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.Nullable;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RestResultWrapper
@RequestMapping(path = "/business")
public class BusinessController {
    @Autowired
    private IBusinessMapper businessMapper;

    /**
     * 办理时限监管
     *
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/task/list/waiting", method = RequestMethod.POST)
    @ResponseBody
    @Operation(summary = "办理时限监管")
    public Object waitingList(@Nullable @RequestBody PageQuery<ActHiTaskInst, TaskVoQuery> request) throws Exception {
        if (request == null) {
            request = new PageQuery<>();
        }
        PageResult<ActHiTaskInst> result = businessMapper.queryWaitingTask(request);
        List<ActHiTaskInst> lists = result.getList();
        for (ActHiTaskInst actHiTaskInst : lists) {
            actHiTaskInst.setBizStatus(DateUtil.getDistanceTime(new Date(), actHiTaskInst.getDueDate()));
        }
        return new Page(request, result);
    }

    /**
     * 办理过程监管
     *
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/task/list/process", method = RequestMethod.POST)
    @ResponseBody
    @Operation(summary = "办理过程监管")
    public Object processList(@Nullable @RequestBody PageQuery<ActHiTaskInst, TaskVoQuery> request) throws Exception {
        if (request == null) {
            request = new PageQuery<>();
        }
        PageResult<ActHiTaskInst> result = businessMapper.queryWaitingTask(request);
        List<ActHiTaskInst> lists = result.getList();
        for (ActHiTaskInst actHiTaskInst : lists) {
            actHiTaskInst.setBizStatus(DateUtil.getDistanceTime(actHiTaskInst.getCreateTime(), new Date()));
        }
        return new Page(request, result);
    }

    /**
     * 办理结果监管
     *
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/task/list/finish", method = RequestMethod.POST)
    @ResponseBody
    @Operation(summary = "办理结果监管")
    public Object finishList(@Nullable @RequestBody PageQuery<ActHiTaskInst, TaskVoQuery> request) throws Exception {
        if (request == null) {
            request = new PageQuery<>();
        }
        PageResult<ActHiTaskInst> result = businessMapper.queryFinishTask(request);
        List<ActHiTaskInst> lists = result.getList();
        for (ActHiTaskInst actHiTaskInst : lists) {
            actHiTaskInst.setBizStatus(DateUtil.getDistanceTime(actHiTaskInst.getCreateTime(), actHiTaskInst.getEndTime()));
        }
        return new Page(request, result);
    }

    /**
     * 绩效监管
     *
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/task/list/performance", method = RequestMethod.POST)
    @ResponseBody
    @Operation(summary = "绩效监管列表")
    public Object performanceList(@Nullable @RequestBody PageQuery<ActHiTaskInst, BusinessQuery> request) throws Exception {
        if (request == null) {
            request = new PageQuery<>();
        }
        PageResult<Map<String, Object>> result = null;
        if (request.getSimpleCondition() != null) {
            if ("org".equals(request.getSimpleCondition().getSelectType())) {
                result = businessMapper.statisticsOrgJxgl(request.getSimpleCondition(), request);
            } else if ("pos".equals(request.getSimpleCondition().getSelectType())) {
                result = businessMapper.statisticsPosJxgl(request.getSimpleCondition(), request);
            } else if ("bus".equals(request.getSimpleCondition().getSelectType())) {
                result = businessMapper.statisticsBusJxgl(request.getSimpleCondition(), request);
            }
        }
        if (result != null) {
            List<Map<String, Object>> lists = result.getList();
            for (Map<String, Object> list : lists) {
                list.put("bizStatus", DateUtil.convertDistanceTime(Long.valueOf(list.get("duration").toString())));
            }
        }
        return new Page(request, result);
    }

    /**
     * 绩效监管
     *
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/task/pie/performance", method = RequestMethod.POST)
    @ResponseBody
    @Operation(summary = "绩效监管图表")
    public Object performancePie(@Nullable @RequestBody PageQuery<ActHiTaskInst, BusinessQuery> request) throws Exception {
        if (request == null) {
            request = new PageQuery<>();
        }
        Map<String, Object> result = null;
        if (request.getSimpleCondition() != null) {
            if ("org".equals(request.getSimpleCondition().getSelectType())) {
                result = businessMapper.statisticsOrgPie(request.getSimpleCondition());
            } else if ("pos".equals(request.getSimpleCondition().getSelectType())) {
                result = businessMapper.statisticsPosPie(request.getSimpleCondition());
            } else if ("bus".equals(request.getSimpleCondition().getSelectType())) {
                result = businessMapper.statisticsBusPie(request.getSimpleCondition());
            }
        }
        return result;
    }

    /**
     * 业务类别监管
     *
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/task/list/category", method = RequestMethod.POST)
    @ResponseBody
    @Operation(summary = "业务类别监管")
    public Object categoryList(@Nullable @RequestBody PageQuery<ActHiTaskInst, BusinessQuery> request) throws Exception {
        if (request == null) {
            request = new PageQuery<>();
        }
        PageResult<Map<String, Object>> result = null;
        if (request.getSimpleCondition() != null) {
            if ("org".equals(request.getSimpleCondition().getSelectType())) {
                String infos = businessMapper.queryOrgLbglColumns(request.getSimpleCondition());
                System.out.println("list------------------------->" + infos);
                Map<String, Object> columns = toMap(toStrArray(infos));
                result = businessMapper.statisticsOrgLbglList(request.getSimpleCondition(), request);
                List<Map<String, Object>> lists = result.getList();
                for (Map<String, Object> list : lists) {
                    List<Map<String, Object>> newColums = new ArrayList<>();
                    Map<String, Object> values = toMap(toStrArray(list.get("infos").toString()));
                    for (String key : columns.keySet()) {
                        Map<String, Object> newColum = new HashMap<>();
                        if (values.containsKey(key)) {
                            newColum.put("name", key);
                            newColum.put("count", values.get(key));
                        } else {
                            newColum.put("name", key);
                            newColum.put("count", 0);
                        }
                        newColums.add(newColum);
                    }
                    list.put("columns", newColums);
                }
            } else if ("pos".equals(request.getSimpleCondition().getSelectType())) {
                String infos = businessMapper.queryPosLbglColumns(request.getSimpleCondition());
                System.out.println("list------------------------->" + infos);
                Map<String, Object> columns = toMap(toStrArray(infos));
                result = businessMapper.statisticsPosLbglList(request.getSimpleCondition(), request);
                List<Map<String, Object>> lists = result.getList();
                for (Map<String, Object> list : lists) {
                    List<Map<String, Object>> newColums = new ArrayList<>();
                    Map<String, Object> values = toMap(toStrArray(list.get("infos").toString()));
                    for (String key : columns.keySet()) {
                        Map<String, Object> newColum = new HashMap<>();
                        if (values.containsKey(key)) {
                            newColum.put("name", key);
                            newColum.put("count", values.get(key));
                        } else {
                            newColum.put("name", key);
                            newColum.put("count", 0);
                        }
                        newColums.add(newColum);
                    }
                    list.put("columns", newColums);
                }
            }
        }
        System.out.println("list------------------------->" + result.getList());
        return new Page(request, result);
    }

    /**
     * 业务类别监管
     *
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/task/bar/category", method = RequestMethod.POST)
    @ResponseBody
    @Operation(summary = "业务类别监管")
    public Object categoryBar(@Nullable @RequestBody PageQuery<ActHiTaskInst, BusinessQuery> request) throws Exception {
        if (request == null) {
            request = new PageQuery<>();
        }
        List<Map<String, Object>> result = null;
        if (request.getSimpleCondition() != null) {
            if ("org".equals(request.getSimpleCondition().getSelectType())) {
                String infos = businessMapper.queryOrgLbglColumns(request.getSimpleCondition());
                System.out.println("bar------------------------->" + infos);
                Map<String, Object> columns = toMap(toStrArray(infos));
                result = businessMapper.statisticsOrgLbglBar(request.getSimpleCondition());
                for (Map<String, Object> list : result) {
                    List<Map<String, Object>> newColums = new ArrayList<>();
                    Map<String, Object> values = toMap(toStrArray(list.get("infos").toString()));
                    for (String key : columns.keySet()) {
                        Map<String, Object> newColum = new HashMap<>();
                        if (values.containsKey(key)) {
                            newColum.put("name", key);
                            newColum.put("count", values.get(key));
                        } else {
                            newColum.put("name", key);
                            newColum.put("count", 0);
                        }
                        newColums.add(newColum);
                    }
                    list.put("columns", newColums);
                }
            } else if ("pos".equals(request.getSimpleCondition().getSelectType())) {
                String infos = businessMapper.queryPosLbglColumns(request.getSimpleCondition());
                System.out.println("bar------------------------->" + infos);
                Map<String, Object> columns = toMap(toStrArray(infos));
                result = businessMapper.statisticsPosLbglBar(request.getSimpleCondition());
                for (Map<String, Object> list : result) {
                    List<Map<String, Object>> newColums = new ArrayList<>();
                    Map<String, Object> values = toMap(toStrArray(list.get("infos").toString()));
                    for (String key : columns.keySet()) {
                        Map<String, Object> newColum = new HashMap<>();
                        if (values.containsKey(key)) {
                            newColum.put("name", key);
                            newColum.put("count", values.get(key));
                        } else {
                            newColum.put("name", key);
                            newColum.put("count", 0);
                        }
                        newColums.add(newColum);
                    }
                    list.put("columns", newColums);
                }
            }
        }
        System.out.println("bar------------------------->" + result);
        return result;
    }

    public Map<String, Object> toMap(String[] strs) {
        Map<String, Object> map = new HashMap<>();
        for (String str : strs) {
            String[] items = toStrArray(":", str);
            if (items.length > 1) {
                map.put(items[0].trim(), items[1].trim());
            }
        }
        return map;
    }

    /**
     * 转换为String数组<br>
     *
     * @param str 被转换的值
     * @return 结果
     */
    public static String[] toStrArray(String str) {
        return toStrArray(",", str);
    }

    /**
     * 转换为String数组<br>
     *
     * @param split 分隔符
     * @param split 被转换的值
     * @return 结果
     */
    public static String[] toStrArray(String split, String str) {
        return str.split(split);
    }
}
